package com.hetao.jmb2c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hetao.jmb2c.dto.GoodsPageQueryDTO;
import com.hetao.jmb2c.entity.Goods;
import com.hetao.jmb2c.mapper.GoodsMapper;
import com.hetao.jmb2c.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public IPage<Goods> getPage(GoodsPageQueryDTO goodsPageQueryDTO) {
        IPage<Goods> pageInfo=new Page<>(goodsPageQueryDTO.getCurrent(), goodsPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(goodsPageQueryDTO.getGoodsNo()), Goods::getGoodsNo, goodsPageQueryDTO.getGoodsNo());
        wrapper.eq(StringUtils.hasText(goodsPageQueryDTO.getGoodsNo()), Goods::getGoodsNo, goodsPageQueryDTO.getGoodsNo());
        wrapper.eq(goodsPageQueryDTO.getCategoryId() != null, Goods::getCategoryId, goodsPageQueryDTO.getCategoryId());
        wrapper.eq(goodsPageQueryDTO.getBrandId() != null, Goods::getBrandId, goodsPageQueryDTO.getBrandId());
        IPage<Goods> page = this.page(pageInfo, wrapper);
        return page;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(status); // 需要在 Goods 实体类中有 status 字段（上架/下架）
        this.updateById(goods);
    }

    @Override
    public void updateVisible(Long id, Integer visible) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setVisible(visible); // 需要在 Goods 实体类中有 visible 字段（是否显示）
        this.updateById(goods);
    }

}
